
/**   
*    
* ��Ŀ��ƣ�a_yongan   
* ����ƣ�DeptServiceImpl   
* ��������   
* �����ˣ�liubo   
* ����ʱ�䣺2018��6��5�� ����11:51:38    
* �޸�ʱ�䣺2018��6��5�� ����11:51:38   
* �޸ı�ע��   
* @version    
*    
*/
package com.yongan.user.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import com.yongan.user.dao.DeptDao;
import com.yongan.user.dto.UserExecution;
import com.yongan.user.entity.Dept;
import com.yongan.user.enums.UserStatEnum;
import com.yongan.user.exception.UserException;
import com.yongan.user.service.DeptService;

/**
 * @author liubo
 *
 */
@Service
public class DeptServiceImpl implements DeptService{

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private DeptDao deptDao;
	private final String slat = "dsjdajsdkHJJHAJAKdsd921e0p12*DAe#@$@!qew";
	/* ���� Javadoc��
	 * @see com.yongan.user.service.DeptService#getDeptList(int, int)
	 */
	@Override
	public List<Dept> getDeptList(int offset, int limit) {
		// TODO �Զ���ɵķ������
		return deptDao.queryAll(offset, limit);
	}

	/* ���� Javadoc��
	 * @see com.yongan.user.service.DeptService#getDeptById(int)
	 */
	@Override
	public Dept getDeptById(String deptId) {
		// TODO �Զ���ɵķ������
		return deptDao.queryById(deptId);
	}

	/* ���� Javadoc��
	 * @see com.yongan.user.service.DeptService#executeDept(com.yongan.user.entity.Dept, int, java.lang.String)
	 */
	@Override
	public UserExecution executeDept(Dept dept, String deptId, String md5)
			throws UserException {
		// TODO �Զ���ɵķ������
		if(md5 == null || !md5.equals(getMD5(deptId))){
			//throw new UserException("user data rewrite");
		}
		try{
			if(dept == null){
				//����Ĳ��Ŷ���Ϊ�գ�����Ĭ��Ϊɾ�����
				int deleteCount = deptDao.deleteDept(deptId);
				if(deleteCount<=0){
					throw new UserException("delete failed");
				}else{
					return new UserExecution(deptId, UserStatEnum.SUCCESS);
				}
			}else if((deptId.equals(""))){
				//����Ĳ��Ŷ���IdΪ�գ�����Ĭ��Ϊ��������
				int addCount = deptDao.insertDept(dept);
				if(addCount<=0){
					throw new UserException("add failed");
				}else{
					return new UserExecution(deptId, UserStatEnum.SUCCESS);
				}
			}else if(!(deptId.equals(""))){
				//������û�����Id��Ϊ�գ�����Ĭ��Ϊ�޸Ĳ���
				int updateCount = deptDao.updateDept(dept);
				if(updateCount<=0){
					throw new UserException("update failed");
				}else{
					return new UserExecution(dept.getDeptId(), UserStatEnum.SUCCESS);
				}
			}else{
				throw new UserException("parameter error");
			}
			
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			throw new UserException("inner error:"+e.getMessage());
		}
	}
	private String getMD5(String userId){
		String base = userId + "/" + slat;
		String md5 = DigestUtils.md5DigestAsHex(base.getBytes());
		return md5;
	}

	/* ���� Javadoc��
	 * @see com.yongan.user.service.DeptService#getDeotCount(java.lang.String, java.lang.String)
	 */
	@Override
	public int getDeptCount(String title, String value) {
		// TODO �Զ���ɵķ������
		return deptDao.queryCount(title, value);
	}

	/* （非 Javadoc）
	 * @see com.yongan.user.service.DeptService#getDeptListTree()
	 */
	@Override
	public List<Dept> getDeptListTree() {
		// TODO 自动生成的方法存根
		return deptDao.queryAllTree();
	}

	/* （非 Javadoc）
	 * @see com.yongan.user.service.DeptService#getDeptListTreeByTopId()
	 */
	@Override
	public List<Dept> getDeptListTreeByTopId(String topId,int offset,int limit) {
		// TODO 自动生成的方法存根
		return deptDao.queryAllTreeByTopId(topId,offset,limit);
	}

	/* （非 Javadoc）
	 * @see com.yongan.user.service.DeptService#queryCountByTopId(java.lang.String)
	 */
	@Override
	public int getCountByTopId(String topId) {
		// TODO 自动生成的方法存根
		return deptDao.queryCountByTopId(topId);
	}
}
